/**
 * @author 程麒阁
 * @description 用于获取管理员权限
 * @version 1.0
 * @time 2021/11/14 14:07
 */
// 需要在这里配置你的route路径
import routes from '../router/routes.js'

export function AuthorOption (childRoutes,keyArr) {
    childRoutes.forEach(item => {
        if (keyArr.indexOf(item.key) < 0) {
            removeOptionByKey(routes, item.key)
        }
        if (item.children) {
            AuthorOption(item.children, keyArr)
        }
    })
}

export function getAuthorOption(childRoutes,keyArr){
    AuthorOption(childRoutes,formatKeys(keyArr))
    return routes
}

export function removeOptionByKey (routes, key) {
    let keyArr = key.split('-')
    let deep = routes[keyArr[0]]
    if (deep) {
        keyArr.slice(1, -1).forEach((item) => {
            deep = deep.children[item]
        })
        if (keyArr.length === 1) {
            delete routes[keyArr[0]]
        } else {
            if(deep){
                delete deep.children[keyArr.pop()]
            }
        }
    }
}

// 根据key值获取功能选项
export function getOptionByKey (routes, key) {
    let keyArr = key.split('-')
    let deep = routes[keyArr[0]]
    try {
        keyArr.slice(1).forEach((item) => {
            deep = deep.children[item]
        })
    } catch (e) {
        deep = {}
    }
    return deep
}

export function formatKeys (keys) {
    let keyArr = []
    keys.forEach((item) => {
        let arr = item.split('-').map((item, index) => index > 0 ? '-' + item : item)
        arr.forEach((item, index) => {
            keyArr.push(arr.slice(0, index + 1).join(''))
        })
    })
    return Array.from(new Set(keyArr))
}

export default routes
